package com.example.teach.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.teach.entity.Major;
import com.example.teach.entity.Result;
import com.example.teach.service.CourseService;
import com.example.teach.service.MajorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/major")
public class MajorController {

    @Autowired
    private MajorService majorService;

    @Autowired
    private CourseService courseService;

    @GetMapping("getById")
    public Result getMajorById(@RequestParam("majorId") Integer majorId) {
        return Result.success(majorService.getById(majorId));
    }

    @PostMapping("/addcourse")
    public Result addCourse(@RequestParam Integer majorId,
                            @RequestParam Integer courseId){
        return Result.success(courseService.addCourse(majorId,courseId));
    }

    @GetMapping("list")
    public Result list(){
        return Result.success(majorService.list());
    }

    @GetMapping("selectByMajorId")
    public Result selectByMajorId(@RequestParam Integer majorId){
        return courseService.selectByMajorId(majorId);
    }

    @DeleteMapping("remove")
    public Result remove(@RequestParam Integer majorId,@RequestParam Integer courseId){
        return courseService.delete(majorId,courseId);
    }

    @GetMapping("/selectall")
    public Result selectAll(){
        QueryWrapper<Major> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", 0);
        List<Major> list = majorService.list(queryWrapper);
        return Result.success(list);
    }


    @PostMapping("/add")
    public Result addMajor(@RequestBody Major major) {
        if(majorService.exists(new QueryWrapper<>(major))){
            return Result.error("专业已经存在");
        }
        majorService.save(major);
        return Result.success();
    }

    @DeleteMapping("/delete")
    public Result deleteMajor(@RequestParam Integer majorId) {
        majorService.removeById(majorId);
        courseService.deleteById(majorId);
        return Result.success();
    }

    @PostMapping("/update")
    public Result updateMajor(@RequestParam("majorId") Integer majorId,@RequestParam("majorName") String majorName) {
        majorService.updateById(majorId,majorName);
        return Result.success();
    }

    @GetMapping("/page")
    public Result page(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                       @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                       @RequestParam(value = "majorName", defaultValue = "") String majorName) {
        LambdaQueryWrapper<Major> queryWrapper = new LambdaQueryWrapper<>();
        if (StrUtil.isNotBlank(majorName)) {
            queryWrapper.like(Major::getMajorName, majorName);
        }
        Page<Major> page = majorService.page(new Page<Major>(pageNum, pageSize), queryWrapper);
        return Result.success(page);
    }

}
